from odoo import api, fields, _, models


class MrpWorkorder(models.Model):
    _inherit = 'mrp.workorder'

    subcontracting_received_qty = fields.Integer(string='Subcontracting Received Qty', copy=False)
    subcontracting_received_pnl_qty = fields.Integer(string='Subcontracting Received Pnl Qty', copy=False)
    note_sb = fields.Text(string='Note', copy=False)
    sub_apply_line_ids = fields.One2many('sb.subcontracting.apply.line', 'workorder_id')

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if self._context.get('sub_apply'):
            ids = self._name_search(name, args, operator, limit=limit, order=self._order)
            records = self.browse(ids)
            display_names = [(rec.id, rec.name) for rec in records]
        else:
            display_names = super().name_search(name, args, operator, limit)
        return display_names

    @api.depends('name', 'production_id', 'write_date')
    def _compute_display_name(self):
        if self._context.get('sub_apply'):
            for obj in self:
                obj.display_name = obj.name
        else:
            return super()._compute_display_name()
